﻿
/*----------------------------------------------------------------
 * 创建时间：2023/8/11 16:58:55
 * 作  者：wenlli 
 * 项  目：Minotaur.App.Modules.Business.Service.Table 
 * 描  述：数据表信息逻辑接口-实现
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时  间：
 * 修改说明：
 * 
 *----------------------------------------------------------------*/


using Minotaur.App.Core;
using Minotaur.App.Core.Attributes;
using Minotaur.App.Domains.Model.Table;
using Minotaur.App.Domains.Request;
using Minotaur.App.Domains.ViewModel.Table;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minotaur.App.Modules.Business.Service.Table
{
    /// <summary>
    /// 数据表信息逻辑接口-实现
    /// </summary>
    public class TableService : ITableService
    {
        private readonly IDbContext dbContext;
        private readonly GlobalUserModel userModel;

        public TableService(IDbContext dbContext, GlobalUserModel userModel)
        {
            this.dbContext = dbContext;
            this.userModel = userModel;
        }
        [GlobalLogAspect]
        public async Task<TableInfoListRespone> IniTableInfos(SearcheRequest searcheRequest)
        {
            TableInfoListRespone respone = new TableInfoListRespone();
            try
            {
                RefAsync<int> total = 0;
                int lan = (int)((userModel.Language + "").ConvertEnumEvent<Domains.Enum.LanguageType>());
                var queryable = this.dbContext.BusinessDb.Queryable<TableInfo>()
                 .LeftJoin<TableInfoRes>((a, b) => a.Autoid == b.TableId && b.Lan == lan);
                if (!string.IsNullOrEmpty(searcheRequest.SearchQuery))
                {
                    queryable.Where((a, b) => a.TableCode.Contains(searcheRequest.SearchQuery) || b.Value.Contains(searcheRequest.SearchQuery));
                }
                List<TableInfoListViewModel> TableInfoList = await queryable
                    .Select((a, b) => new TableInfoListViewModel()
                    {
                        Autoid = a.Autoid,
                        Enable = a.Enable,
                        FlowTableCode = a.FlowTableCode,
                        GroupType = a.GroupType,
                        Isflow = a.Isflow,
                        IsPrimary = a.IsPrimary,
                        IsSystem = a.IsSystem,
                        PkTable = a.PkTable,
                        Records = a.Records,
                        Sequence = a.Sequence,
                        TableCode = a.TableCode,
                        ViewType = a.ViewType,
                        TableName = b.Value,
                        Creator = a.Creator.ToString(),
                        CreatDate = a.CreatDate.HasValue ? a.CreatDate.Value.ToString("yyyy-MM-dd HH:mm") : ""
                    }).ToOffsetPageAsync(searcheRequest.PageIndex, searcheRequest.PageSize, total);
                respone.TotalCount = total.Value;
                respone.List = TableInfoList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return respone;
        }
    }
}
